package com.jiezheng.huinong.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jiezheng.huinong.dao.ApplyManageDAOImpl;
import com.jiezheng.huinong.model.ApplyInfo;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.util.JqPage;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ApplyInfoAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7863444449611106630L;

	private JqPage pageInfo = new JqPage();
	private List<ApplyInfo> applies = new ArrayList<ApplyInfo>();
	private String sord = "";
	private String sidx = "";
	
	private ApplyInfo cond = new ApplyInfo();

	/**
	 * 列出所有审请
	 * 
	 * @return
	 */
	public String listApplyInfo() {

		//设定检索部门条件
		cond.setDeptNo(deptNoCondition());
		
		// 检验当前用户的部门
		List<Integer> statusRange = new ArrayList<Integer>();
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");
		
		// 国库股
		if (deptNo.equals("003") ) {
			statusRange.add(OperationStatus.FINAL_AUDITED);
			statusRange.add(OperationStatus.SUBMITED);
			statusRange.add(OperationStatus.EXECUTED);
		}
		
		// 中国人民银行应可以看到所有申请
		if (deptNo.equals("800") ) {
			statusRange.add(OperationStatus.SUBMITED);
			statusRange.add(OperationStatus.EXECUTED);
		}
		
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyNum(cond, statusRange);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applies = (new ApplyManageDAOImpl()).getApplyInfo(cond, statusRange, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}
	
	public String noJointApplyInfo() {
		
		//设定检索部门条件
		cond.setDeptNo(deptNoCondition());
		List<Integer> statusRange = new ArrayList<Integer>();
		statusRange.add(OperationStatus.NO_JONIT);
		
		Integer total = (new ApplyManageDAOImpl()).getTotalApplyNum(cond, statusRange);
		Integer totalPage = (Integer) (total / pageInfo.getRows())
				+ ((total % pageInfo.getRows() == 0) ? 0 : 1);
		pageInfo.setTotalPage(totalPage);
		pageInfo.setRecord(total);
		// 取得当前页
		Integer curPage = pageInfo.getPage();
		Integer startRow = (curPage - 1) * pageInfo.getRows();

		applies = (new ApplyManageDAOImpl()).getApplyInfo(cond, statusRange, startRow,
				pageInfo.getRows());
		return SUCCESS;
	}
	
	private String deptNoCondition(){
		// 检验当前用户的部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");

		// 财政所用户处理
		if (deptNo.startsWith("2")) {
			return deptNo;
		}

		// 国库股/中国人民银行应可以看到所有申请
		else if (deptNo.equals("003") || deptNo.equals("800")) {
			deptNo = "";
		}
		return deptNo;
	}

	public JqPage getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(JqPage pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<ApplyInfo> getApplies() {
		return applies;
	}

	public void setApplies(List<ApplyInfo> applies) {
		this.applies = applies;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public ApplyInfo getCond() {
		return cond;
	}

	public void setCond(ApplyInfo cond) {
		this.cond = cond;
	}

}
